package org.kongqian.basic.controller;

import org.kongqian.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastdfs")
public class FastdfsController {

    @Value("${fastdfs.url}")
    private String fastdfsurl;

    @Autowired
    private IFastDfsService fastDfsService;

    @PostMapping("/upload")
    public AjaxResult uploadFasdfs(MultipartFile file){       //接收文件 以前用httpservletrequest来接收
        try {
            String path = fastDfsService.upload(file);
            return AjaxResult.getResult().setObject(fastdfsurl+path);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("上传图片失败");
        }
    }

    @DeleteMapping("/delete")
    // this.$http.delete("/fastdfs/delete/?path="+filePath) 用？号拼接我们直接接收参数就好了
    public AjaxResult deleteFasdfs(String path){
        try {
            fastDfsService.deleteFasdfs(path);
            return AjaxResult.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("删除图片失败");
        }
    }
}
